package uk.co.q3c.deplan.client.service;

import java.util.logging.Logger;

public class ClientServiceFactory implements ServiceFactory {
	protected static final Logger logger = Logger.getLogger(ClientServiceFactory.class.getName());
	private CalendarServiceClient calendarService;
	private DesignServiceClient designService;
	private ResourceServiceClient resourceService;
	private TaskService taskService;
	private AsyncServiceFactory asyncServicefactory;

	// private TaskService2Client taskService2;

	@Override
	public CalendarServiceClient calendarService() {
		if (calendarService == null) {
			calendarService = new CalendarServiceClient();
			checkAsyncFactory();
			calendarService.setRpc(asyncServicefactory.calendarServiceAsync());
		}
		return calendarService;
	}

	private void checkAsyncFactory() {
		if (asyncServicefactory == null) {
			String msg = "Async service factory has not been set";
			logger.severe(msg);
			throw new RuntimeException(msg);
		}

	}

	@Override
	public DesignServiceClient designService() {
		if (designService == null) {
			designService = new DesignServiceClient();
			checkAsyncFactory();
			designService.setRpc(asyncServicefactory.designServiceAsync());
		}
		return designService;
	}

	@Override
	public ResourceServiceClient resourceService() {
		if (resourceService == null) {
			resourceService = new ResourceServiceClient();
			checkAsyncFactory();
			resourceService.setRpc(asyncServicefactory.resourceServiceAsync());
		}
		return resourceService;
	}

	@Override
	public TaskService taskService() {
		// if (taskService == null) {
		// taskService = new TaskServiceClient();
		// checkAsyncFactory();
		// taskService.setRpc(asyncServicefactory.taskserviceAsync());
		// }
		throw new RuntimeException("not yet implemented");
		// return taskService;
	}

	@Override
	public void closeSession() {
		throw new RuntimeException("Not yet implemented");

	}

	@Override
	public void openSession() {
		throw new RuntimeException("Not yet implemented");

	}

	public AsyncServiceFactory getAsyncServicefactory() {
		return asyncServicefactory;
	}

	public void setAsyncServicefactory(AsyncServiceFactory asyncServicefactory) {
		this.asyncServicefactory = asyncServicefactory;
	}

	// @Override
	// public TaskService2 taskService2() {
	// if (taskService2 == null) {
	// taskService2 = new TaskService2Client();
	// }
	// return taskService2;
	// }

}
